package com.zhu.storesmanager.views;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.utils.ViewsUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MoreRadioGroup extends LinearLayout{

	private RadioGroup mRadioGroup1 = null; //第一组
	private RadioGroup mRadioGroup2 = null; //第二组

	public MoreRadioGroup(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public MoreRadioGroup(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public MoreRadioGroup(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	/**
	 * (non-Javadoc)
	 * @see android.view.View#onFinishInflate()
	 */
	public void onFinishInflate(){
		mRadioGroup1 = (RadioGroup)findViewById(R.id.radiogroup1);
		mRadioGroup2 = (RadioGroup)findViewById(R.id.radiogroup2);
		mRadioGroup1.setOnCheckedChangeListener(mOnCheckedChangeListener);
		mRadioGroup2.setOnCheckedChangeListener(mOnCheckedChangeListener);
	}
	
	/**
	 * 设置标题
	 * @param titleID
	 */
	public void setTitle(int titleID){
		((TextView)findViewById(R.id.radiogroup_label)).setText(titleID);
	}

	/**
	 * 设置标题
	 * @param title
	 */
	public void setTitle(String title){
		((TextView)findViewById(R.id.radiogroup_label)).setText(title);
	}
	/**
	 * RadioGroup 选中变化
	 */
	OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener(){
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if (group != null) {
                if (group == mRadioGroup1) {
                    if(checkedId==R.id.radiobutton1||checkedId==R.id.radiobutton2){
                    	mRadioGroup2.clearCheck();
                        group.check(checkedId);
                    }
                } else if (group == mRadioGroup2) {
                    if(checkedId==R.id.radiobutton3||checkedId==R.id.radiobutton4||checkedId==R.id.radiobutton5){
                    	mRadioGroup1.clearCheck();
                        group.check(checkedId);
                    }
                }
            }
        }
	};

	/**
	 * 获取选中项索引
	 * @return index 索引
	 */
	public int getSelectedIndex(){
		int index1 = mRadioGroup1.getCheckedRadioButtonId();
		int index2 = mRadioGroup2.getCheckedRadioButtonId();
		if(index1 == -1 && index2 == -1){
			return index1;
		}else if(index1 != -1 && index2 == -1){
			return mRadioGroup1.indexOfChild(mRadioGroup1.findViewById(index1));
		}else if(index1 == -1 && index2 != -1){
			return  2 + mRadioGroup2.indexOfChild(mRadioGroup2.findViewById(index2));
		}
		return -1;
	}

	/**
	 * 设置选中项
	 * @param index 选中index
	 */
	public void setSelectedIndex(int index){
		if(index == -1){ //未选中
			return;
		}
		if(index < 2){
			mRadioGroup1.check(mRadioGroup1.getChildAt(index).getId());   
		}else{
			mRadioGroup2.check(mRadioGroup2.getChildAt(index -2).getId());
		}
	}

	/**
	 * 禁用RadioGroup
	 */
	public void setRadioGroupdisable(){
		ViewsUtil.setRadioGroupdisable(mRadioGroup1);
		ViewsUtil.setRadioGroupdisable(mRadioGroup2);
	}
}
